<?php

namespace App\Core\Api;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Pagination\LengthAwarePaginator;
use Response;
use Symfony\Component\HttpFoundation\Response as HttpCode;

class ApiResponse
{
    /**
     * @param $data
     * @param string $message
     * @param $code
     * @param array $header
     * @return mixed
     */
    public function success($data, $message = '操作成功', $code = SUCCESS_CODE, $header = [])
    {
        if ($this->debugEnabled()) {
            $data = array_merge($data, $this->getDebug());
        }
        return Response::json([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ], HttpCode::HTTP_OK, $header);
    }

    /**
     * @param string $message
     * @param int $code
     * @param array $data
     * @param array $header
     * @return mixed
     */
    public function failed($message = '操作失败', $code = ERROR_CODE, $data = [], $header = [])
    {
        if ($this->debugEnabled()) {
            $data = array_merge($data, $this->getDebug());
        }
        return Response::json([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ], HttpCode::HTTP_OK, $header);
    }

    protected function debugEnabled()
    {
        return app()->has('debugbar') && app('debugbar')->isEnabled();
    }

    protected function getDebug()
    {
        return ['_debugbar' => app('debugbar')->getData()];
    }
}
